#include "auth.h"
int next_word(char *message,int num)
{
        int i=0;
        while(isalnum(*message) && i<num)
        {
                i++;
                message++;
        }

        return i;
}
int next_num(char *message)
{
        int i=0;
        char temp[6];
        char *start=message;
        while(isdigit(*message) )
        {
                i++;
                message++;
        }

        if(i>0)
        {
                strncpy(temp,start,i);
                return atoi(temp);
        }


        return 0;
}
int compile(CAUTHLOG *ca)
{
        int flag;
        flag=regcomp(&ca->status,RE_STATUS,REG_EXTENDED|REG_ICASE);
        if(flag)
        {
			printf("Error compiling regex");
			return -1;
		}

        flag=regcomp(&ca->user,RE_USER,REG_EXTENDED|REG_ICASE);
        if(flag)
        {
                printf("Error compiling regex");
                return -1;
        }
        flag=regcomp(&ca->uid,RE_UID,REG_EXTENDED|REG_ICASE);
        if(flag)
        {
                printf("Error compiling regex");
                return -1;
        }
        flag=regcomp(&ca->ip,RE_IP4,REG_EXTENDED|REG_ICASE);
        if(flag)
        {
                printf("Error compiling regex");
                return -1;
        }
        flag=regcomp(&ca->timestamp,RE_TIMESTAMP,REG_EXTENDED|REG_ICASE);
        if(flag)
        {
                printf("Error compiling regex");
                return -1;
        }
        flag=regcomp(&ca->port,RE_PORT,REG_EXTENDED|REG_ICASE);
        if(flag)
        {
                printf("Error compiling regex");
                return -1;
        }
        flag=regcomp(&ca->subject,RE_MISC,REG_EXTENDED|REG_ICASE);
        if(flag)
        {
			printf("Error compiling regex");
			return -1;
		}
        return 0;
}
int mmatch(AUTHLOG *temp,CAUTHLOG *ca,char *str)
{
        int flag,offset1,offset2;
        regmatch_t match_ptr;
        size_t nmatch=1;
        memset(temp,0,sizeof(AUTHLOG));


        flag=regexec(&ca->status,str,nmatch,&match_ptr,0);
        if(!flag)
        {
			strncpy(temp->status,str+match_ptr.rm_so,match_ptr.rm_eo-match_ptr.rm_so);
		}
        flag=regexec(&ca->user,str,nmatch,&match_ptr,0);
        if(!flag)
        {
                strncpy(temp->user,str + match_ptr.rm_eo, next_word(str +match_ptr.rm_eo,UNAME_MAX));
        }
        flag=regexec(&ca->uid,str,nmatch,&match_ptr,0);
        if(!flag)
        {
                sscanf(str+match_ptr.rm_eo+1,"%d",&temp->uid);
        }
        flag=regexec(&ca->ip,str,nmatch,&match_ptr,0);
        if(!flag)
        {
                strncpy(temp->ip,str +match_ptr.rm_so, match_ptr.rm_eo- match_ptr.rm_so);
        }
        flag=regexec(&ca->timestamp,str,nmatch,&match_ptr,0);
        if(!flag)
        {
                offset1=match_ptr.rm_eo-match_ptr.rm_so;
                strncpy(temp->timestamp,str +match_ptr.rm_so,offset1);
        }
        offset1=next_word(str +match_ptr.rm_eo+1,UNAME_MAX);
        if(offset1>0)
            strncpy(temp->destination,str+match_ptr.rm_eo+1,offset1+1);
        offset2=next_word(str+match_ptr.rm_eo+offset1+2,UNAME_MAX);
        if(offset2>0)
            strncpy(temp->process,str+match_ptr.rm_eo+offset1+1,offset2+1);
        flag=regexec(&ca->port,str,nmatch,&match_ptr,0);
        if(!flag)
        {
                sscanf(str+match_ptr.rm_eo+1,"%d",&temp->port);
        }
        flag=regexec(&ca->subject,str,nmatch,&match_ptr,0);
        if(!flag)
        {
			strncpy(temp->subject,str+match_ptr.rm_eo+1,next_word(str+match_ptr.rm_eo+1,UNAME_MAX));
		}

        return 0;
}
int normalise(bson *b,char *str)
{
    CAUTHLOG ca;
    AUTHLOG a;
    memset(&a,0,sizeof(AUTHLOG));
    if(compile(&ca)==-1) return -1;
    mmatch(&a,&ca,str);
    bson_init(b);
    bson_append_string(b,"timestamp",a.timestamp);
    bson_append_string(b,"who",a.subject);
    bson_append_string(b,"sourceIP",a.ip);
    bson_append_int(b,"sourcePort",a.port);
    bson_append_string(b,"destinationIP",a.destination);
    bson_append_string(b,"generator",a.process);
    bson_append_string(b,"result",a.status);
    bson_finish(b);
    return 0;

}

